package com.lp.biz.operations.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lp.biz.content.mapper.BizCoreContentMapper;
import com.lp.biz.content.model.entity.BizCoreContent;
import com.lp.biz.operations.model.entity.BizRecommendBanner;
import com.lp.biz.operations.mapper.BizRecommendBannerMapper;
import com.lp.biz.operations.model.vo.BannerAdd;
import com.lp.biz.operations.model.vo.BannerList;
import com.lp.biz.operations.service.BizRecommendBannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lp.common.component.RedisService;
import com.lp.common.entity.PageParams;
import com.lp.common.exception.BaseException;
import com.lp.common.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.lp.common.constant.BannerConstant.*;
import static com.lp.common.constant.CacheConstant.BIZ_HOME_KEY;
import static com.lp.common.constant.CommonConstant.NEGATE;
import static com.lp.common.constant.CommonConstant.SURE;
import static com.lp.common.utils.Tools.copyPage;

/**
 * @program: backend
 * @description: BizRecommendBanner 服务实现类
 * @author Ke.Song
 * @since 2024-07-14 23:26:00
 */
@Service
public class BizRecommendBannerServiceImpl extends ServiceImpl<BizRecommendBannerMapper, BizRecommendBanner> implements BizRecommendBannerService {

    @Autowired
    private RedisService redis;

    @Autowired
    private BizCoreContentMapper coreContentMapper;

    @Override
    public CommonResult getBannerList(PageParams query) {
        PageHelper.startPage(query.getPage(), query.getSize());
        List<BizRecommendBanner> list = list(new QueryWrapper<BizRecommendBanner>().lambda()
                .eq(BizRecommendBanner::getIsDelete, NEGATE).orderByDesc(BizRecommendBanner::getCreateAt));
        Page<BannerList> bannerLists = copyPage(new Page<>(), list);
        for (BizRecommendBanner bizRecommendBanner : list) {
            bannerLists.add(BannerList.builder(bizRecommendBanner));
        }
        return CommonResult.restPage(bannerLists);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteBanner(Integer bannerId) {
        BizRecommendBanner banner = getById(bannerId);
        if (banner == null || banner.getIsDelete().equals(SURE)) {
            throw new BaseException("Banner选择错误");
        }
        banner.setIsDelete(SURE);
        updateById(banner);
        redis.delete(BIZ_HOME_KEY);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void addOrUpdateBanner(BannerAdd params) {
        if (params.getId() == null) {
            BizRecommendBanner banner = new BizRecommendBanner();
            checkBanner(banner, params);
            banner.setSort(params.getSort() != null ? params.getSort() : getBaseMapper().getMaxSort());
            save(banner);
        } else {
            BizRecommendBanner banner = getById(params.getId());
            if (banner == null || banner.getIsDelete().equals(SURE) || banner.getIsDisplay().equals(NEGATE)) {
                throw new BaseException("Banner输入错误");
            }
            checkBanner(banner, params);
            LambdaUpdateWrapper<BizRecommendBanner> updateWrapper = new UpdateWrapper<BizRecommendBanner>().lambda()
                    .eq(BizRecommendBanner::getId, banner.getId())
                    .set(BizRecommendBanner::getImageUrl, banner.getImageUrl())
                    .set(BizRecommendBanner::getIsExternal, banner.getIsExternal())
                    .set(BizRecommendBanner::getLink, banner.getLink())
                    .set(BizRecommendBanner::getBizContentId, banner.getBizContentId())
                    .set(BizRecommendBanner::getBizCategoryId, banner.getBizCategoryId())
                    .set(BizRecommendBanner::getBizCategoryCode, banner.getBizCategoryCode())
                    .set(BizRecommendBanner::getIsDisplay, banner.getIsDisplay())
                    .set(BizRecommendBanner::getStartAt, banner.getStartAt())
                    .set(BizRecommendBanner::getEndAt, banner.getEndAt())
                    .set(BizRecommendBanner::getCreateAt, banner.getCreateAt())
                    .set(params.getSort() != null, BizRecommendBanner::getSort, params.getSort())
                    .set(BizRecommendBanner::getIsDelete, banner.getIsDelete());
            update(updateWrapper);
        }
        redis.delete(BIZ_HOME_KEY);
    }

    private void checkBanner(BizRecommendBanner banner, BannerAdd params) {
        if (params.getStartAt().isAfter(params.getEndAt())) {
            throw new BaseException("时间输入错误");
        }
        banner.setStartAt(params.getStartAt());
        banner.setEndAt(params.getEndAt());
        String type = params.getType();
        banner.setImageUrl(params.getImageUrl());
        if (type.equals(TYPE_LINK)) {
            if (params.getLink() == null) {
                throw new BaseException("链接Url不能为空");
            }
            banner.setLink(params.getLink());
            banner.setIsExternal(SURE);
            banner.setBizContentId(null);
            banner.setBizCategoryCode(null);
            banner.setBizCategoryId(null);
        } else if (type.equals(TYPE_READING) || type.equals(TYPE_TRAINING) || type.equals(TYPE_STORY)) {
            BizCoreContent content = coreContentMapper.selectById(params.getContentId());
            if (content == null || content.getIsDeleted().equals(SURE) || content.getIsDisplay().equals(NEGATE) || !content.getBizCategoryCode().equals(type)) {
                throw new BaseException("内容输入错误");
            }
            banner.setBizContentId(content.getId());
            banner.setBizCategoryCode(content.getBizCategoryCode());
            banner.setBizCategoryId(content.getBizCategoryId());
            banner.setIsExternal(NEGATE);
            banner.setLink(null);
        } else {
            throw new BaseException("类型输入错误");
        }
    }
}
